﻿Imports Sunts.DataLayer.SQLite
Imports System.Data.SQLite

Public Class BackupJob
    Public Property BackupJobID As Int32
    Public Property BackupDeviceID As Int32
    Public Property VirtualMachineID As Int32
    Public Property Deleted As Boolean
    Public Property BackupRecurrenceType As Int32
    Public Property ActivationDate As DateTime
    Public Property ExpirationDate As DateTime?
    Public Property BackupTime As DateTime
    Public Property JobName As String
    Public Property Retention As Int32
    Public Function Insert(ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "INSERT INTO BackupJobs (BackupDeviceID, VirtualMachineID, Deleted, BackupRecurrenceType, ActivationDate, ExpirationDate, BackupTime, JobName, Retention) " & _
                                    "VALUES (@BackupDeviceID, @VirtualMachineID, @Deleted, @BackupRecurrenceType, @ActivationDate, @ExpirationDate,@BackupTime, @JobName, @Retention)"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupDeviceID", Me.BackupDeviceID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@VirtualMachineID", Me.VirtualMachineID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Deleted", Me.Deleted))
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupRecurrenceType", Me.BackupRecurrenceType))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ActivationDate", Me.ActivationDate))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ExpirationDate", Me.ExpirationDate))
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupTime", Me.BackupTime))
                sdl.ParameterCollection.Add(New SQLiteParameter("@JobName", Me.JobName))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Retention", Me.Retention))

                Insert = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("BackupJob: Insert()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try

    End Function

    Public Shared Function SelectByID(BackupJobID As Int32, ConnectionString As String) As BackupJob
        Dim BackupJob As BackupJob
        Dim Reader As SQLiteDataReader

        Try
            BackupJob = New BackupJob
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM BackupJobs WHERE BackupJobID = @BackupJobID"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupJobID", BackupJobID))

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    BackupJob.BackupJobID = CInt(Reader("BackupJobID"))
                    BackupJob.BackupDeviceID = CInt(Reader("BackupDeviceID"))
                    BackupJob.VirtualMachineID = CInt(Reader("VirtualMachineID"))
                    BackupJob.Deleted = CBool(Reader("Deleted"))
                    BackupJob.BackupRecurrenceType = CInt(Reader("BackupRecurrenceType"))
                    BackupJob.ActivationDate = CDate(Reader("ActivationDate"))
                    BackupJob.ExpirationDate = CDate(Reader("ExpirationDate"))
                    BackupJob.BackupTime = CDate(Reader("BackupTime"))
                    BackupJob.JobName = CStr(Reader("JobName"))
                    BackupJob.Retention = CInt(Reader("Retention"))
                End While
                Reader.Close()
            End Using

            SelectByID = BackupJob

        Catch ex As Exception
            Throw New Exception("BackupJob: SelectByID()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupJob = Nothing
            Reader = Nothing
        End Try
    End Function

End Class

